package com.chaoli.reggie_diy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chaoli.reggie_diy.common.R;
import com.chaoli.reggie_diy.domain.Category;
import com.chaoli.reggie_diy.domain.Dish;
import com.chaoli.reggie_diy.domain.Setmeal;
import com.chaoli.reggie_diy.service.CategoryService;
import com.chaoli.reggie_diy.service.DishService;
import com.chaoli.reggie_diy.service.SetmealService;
import javafx.scene.control.Pagination;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     *
     * @param category 类别信息的封装
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Category category) {
        log.info("新增分类，分类信息为：{}", category.toString());
        categoryService.save(category);
        return R.success("添加分类成功！！");
    }

    /**
     * 类别信息分页查询
     *
     * @param page     当前页
     * @param pageSize 每页显示数量
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> page(@RequestParam(value = "page") int page, @RequestParam(value = "pageSize") int pageSize) {
        log.info("当前页={}，每页显示数据数量={}", page, pageSize);
        Page<Category> categoryPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Category::getName);
        categoryService.page(categoryPage, lambdaQueryWrapper);
        return R.success(categoryPage);
    }

    /**
     * 根据id删除分类
     * 删除分类前需要判断该分类底下是否关联了菜品或者套餐
     * 如果关联了就不能删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam(value = "id") Long id) {
        log.info("删除分类，id为:{}", id);
        categoryService.deleteUseId(id);
        return R.success("删除分类成功！！");
    }

    /**
     * 修改分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        log.info("修改分类信息，category为：{}", category.toString());
        categoryService.updateById(category);
        return R.success("修改成功!!");
    }

    /**
     * 根据条件查询分类数据
     *
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        queryWrapper.orderByDesc(Category::getUpdateTime);
        List<Category> categoryList = categoryService.list(queryWrapper);
        return R.success(categoryList);

    }

}
